package com.wm.controller;

import com.wm.entity.ResponseResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("/upload")
public class UploadController {
    @Value("${images.location}")
    private String uploadLocation;

    @PostMapping("/avatar")
    public ResponseResult<String> uploadAvatar(MultipartFile file){
        String newFileName="1";
        String suffix=".png";
        File f=new File(uploadLocation+"/"+newFileName+suffix);
        try {
            file.transferTo(f);
        }catch (IOException e){
            e.printStackTrace();
            return ResponseResult.ERROR;
        }
        return new ResponseResult(newFileName+suffix);
    }

}
